<?php

class Dashboard_MenuSuccessView extends SrsDashboardBaseView
{
	public function executeHtml(AgaviRequestDataHolder $rd)
	{
		$this->setupHtml($rd);
		$this->setAttribute('menu', $this->generateMenu());
	}

	private function generateMenu()
	{
		$currentModule = $this->extractCurrentModule();

		$menu = array();

		foreach (AgaviConfig::get('app.menu') as $item)
		{
			if (!empty($item['credential']) && !$this->user->hasCredential($item['credential']))
			{
				continue;
			}
			
			$item['active'] = $item['module'] === $currentModule;
			$item['href']   = $this->getContext()->getRouting()->gen($item['route']);
			
			$menu[] = $item;
		}

		return $menu;
	}

	private function extractCurrentModule()
	{
		$matchedRoutes = $this->getContext()->getRequest()->getAttribute('matched_routes', 'org.agavi.routing');

		if (!isset($matchedRoutes[0]))
		{
			return '';
		}
		
		$route = $this->getContext()->getRouting()->getRoute($matchedRoutes[0]);

		return $route['opt']['module'];
	}
}